﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Ink;
using System.IO;
using ChatRoom.ChatRoomService;

namespace ChatRoom.Views
{
    public partial class InkControl : UserControl
    {
        private Stroke newStroke;

        private Color color = Colors.Black;
        private double width = 1;

        public InkControl()
        {
            InitializeComponent();
            this.SetBoundary();
            GlobalData.ChatRoomServiceClient.OnInkStrokesUpdateReceived += new EventHandler<OnInkStrokesUpdateReceivedEventArgs>(ChatRoomServiceClient_OnInkStrokesUpdateReceived);
        }


        private void inkPresenter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.inkPresenter.CaptureMouse();

            StylusPointCollection stylusPointCollection = new StylusPointCollection();
            stylusPointCollection.Add(e.StylusDevice.GetStylusPoints(this.inkPresenter));
            newStroke = new Stroke(stylusPointCollection);
            newStroke.DrawingAttributes.Color = this.color;
            newStroke.DrawingAttributes.Height = this.width;
            newStroke.DrawingAttributes.Width = this.width;
            this.inkPresenter.Strokes.Add(newStroke);
        }

        private void inkPresenter_LostMouseCapture(object sender, MouseEventArgs e)
        {
            if (newStroke != null)
            {
                GlobalData.ChatRoomServiceClient.SaveInkPresenterAsync(this.GetStrokeModel(this.newStroke));
                newStroke = null;
            }
        }

        private void inkPresenter_MouseMove(object sender, MouseEventArgs e)
        {
            if (newStroke != null)
                newStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(this.inkPresenter));
        }

        private void SetBoundary()
        {
            RectangleGeometry rectangleGeometry = new RectangleGeometry();
            rectangleGeometry.Rect = new Rect(0, 0, this.inkPresenter.ActualWidth, this.inkPresenter.ActualHeight);
            this.inkPresenter.Clip = rectangleGeometry;
        }

        private void red_Click(object sender, RoutedEventArgs e)
        {
            if (this.red.IsChecked == true)
            {
                this.green.IsChecked = false;
                this.color = Colors.Red;
            }
            else
            {
                this.color = Colors.Black;
            }
        }

        private void green_Click(object sender, RoutedEventArgs e)
        {
            if (this.green.IsChecked == true)
            {
                this.red.IsChecked = false;
                this.color = Colors.Green;
            }
            else
            {
                this.color = Colors.Black;
            }
        }

        private void inkWH_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (inkWH != null)
            {
                this.width = inkWH.Value;
            }
        }

        private StrokeModel GetStrokeModel(Stroke stroke)
        {
            StrokeModel strokeModel = new StrokeModel();

            strokeModel.R = stroke.DrawingAttributes.Color.R;
            strokeModel.G = stroke.DrawingAttributes.Color.G;
            strokeModel.A = stroke.DrawingAttributes.Color.A;
            strokeModel.B = stroke.DrawingAttributes.Color.B;

            strokeModel.Height = stroke.DrawingAttributes.Height;
            strokeModel.Height = stroke.DrawingAttributes.Width;
            strokeModel.UserName = GlobalData.UserName;

            strokeModel.Points = new System.Collections.ObjectModel.ObservableCollection<InkPointModel>();

            foreach (var item in stroke.StylusPoints)
            {
                strokeModel.Points.Add(new InkPointModel() { P = item.PressureFactor, X = item.X, Y = item.Y });
            }

            return strokeModel;
        }

        private Stroke GetStroke(StrokeModel strokeModel)
        {
            Stroke stroke = new Stroke();

            stroke.DrawingAttributes.Color = new Color() { R = strokeModel.R, G = strokeModel.G, B = strokeModel.B, A = strokeModel.A };

            stroke.DrawingAttributes.Height = strokeModel.Height;
            stroke.DrawingAttributes.Width = strokeModel.Height;

            foreach (var item in strokeModel.Points)
            {
                stroke.StylusPoints.Add(new StylusPoint() { PressureFactor = item.P, X = item.X, Y = item.Y });
            }

            return stroke;

        }

        void ChatRoomServiceClient_OnInkStrokesUpdateReceived(object sender, OnInkStrokesUpdateReceivedEventArgs e)
        {
            if (e.strokeModel != null)
            {
                this.inkPresenter.Strokes.Add(this.GetStroke(e.strokeModel));
            }
        }

    }
}
